package cn.my.android_demo.model;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MainViewModel extends AndroidViewModel {
//    private String phoneInfo = "";
    final static String TAG=MainViewModel.class.getName();
    //liveData
    MutableLiveData<String> phoneInfo;
    Context context;

    public MainViewModel(@NonNull Application application) {
        super(application);
        context =application;
    }

    public MutableLiveData<String> getPhoneInfo(){
        if (phoneInfo==null){
            phoneInfo=new MutableLiveData<>();
        }
        return phoneInfo;
    }

    //输入
    public void appendNumber(String number){
        phoneInfo.setValue(phoneInfo.getValue()+number);
        Log.e(TAG, "appendNumber: "+phoneInfo.getValue());
    }
    public void backspace(){
        int length =phoneInfo.getValue().length();
        if (length>0){
            phoneInfo.setValue(phoneInfo.getValue().substring(0,length-1));
        }
    }
    public void clear(){
        phoneInfo.setValue("");
    }
}
